import express from 'express';
import cors from 'cors';
import config from './config.js';
import routes from './src/routes/index.js';
import { errorHandler, notFoundHandler, imageProcessor } from './src/middlewares/index.js';

// 创建 Express 应用
const app = express();

// CORS 配置
const corsOptions = {
    origin: function (origin, callback) {
        if (!origin || config.cors.allowedOrigins.includes(origin)) {
            callback(null, true);
        } else {
            callback(null, false);
        }
    },
    optionsSuccessStatus: 200,
};

app.use(cors(corsOptions));
app.use('/src', imageProcessor);
app.use('/src', express.static(config.uploadPath, { 
    maxAge: config.cache.maxAge * 1000
}));
app.use(routes);

// 404 处理
app.use(notFoundHandler);

// 错误处理
app.use(errorHandler);

app.listen(config.port, () => {
    console.log(`DFS is running at http://127.0.0.1:${config.port}`);
});

export default app;